﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Net;
using System.Runtime.InteropServices;
namespace Pharmacity_Price
{
    public partial class Formmain : Form
    {
        [DllImport("Winspool.drv")]
        private static extern bool SetDefaultPrinter(string printerName);
        PageSettings PrintPageSettings = new System.Drawing.Printing.PageSettings();
        public Formmain()
        {        
            InitializeComponent();
            foreach (string printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
            {
                combo_makemoney.Items.Add(printer);
                combo_temprice.Items.Add(printer);
            }
            this.ActiveControl = textBox_datamatrix;
            mode1.Checked = true;
            textBox_number.Enabled = false;
            textBox_number.BackColor = Color.White;
            comboBox_pricemode.SelectedIndex = 2;
        //set setting page
            PrintPageSettings.Margins.Left = 0;
            PrintPageSettings.Margins.Right = 0;
            PrintPageSettings.Margins.Top = 10;
            PrintPageSettings.Margins.Bottom = 0;
            Font PrintFont = new Font("Arial", 5);
            Price.Font = PrintFont;
            PrintTextControl.DefaultPageSettings = PrintPageSettings;
            PreviewPrint.Document = PrintTextControl;
        }        
        private void button_Font_Click(object sender, EventArgs e) // xử lý button edit font
        {
            DialogResult result = FontPick.ShowDialog();
            if (result == DialogResult.OK)
            {
                // Set Font.
                Price.Font = FontPick.Font;
                // Set color
                Price.ForeColor = FontPick.Color;
            }
        }

        private void button_setuppage_Click(object sender, EventArgs e)
        {
            try
            {
                pageSetup.PageSettings = PrintPageSettings;
                pageSetup.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /*
         *  Tin tem giá
         */
        private void TextPrint(object sender, PrintPageEventArgs e)
        {
            e.Graphics.DrawString(Price.Text, new Font(Price.Font, Price.Font.Style), Brushes.Black, 120, 120);
            e.HasMorePages = false;
        }
        private void BeginTextPrint_Click(object sender, EventArgs e)
        {
            if (PrintWin.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                try
                {
                    PrintTextControl.DefaultPageSettings = PrintPageSettings;
                    PreviewPrint.Document = PrintTextControl;

                    PrintTextControl.PrintPage += this.TextPrint;
                    SetDefaultPrinter(combo_temprice.SelectedItem.ToString());
                    PrintTextControl.Print();
                    SetDefaultPrinter(combo_makemoney.SelectedItem.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void PreviewText_Click(object sender, EventArgs e)
        {
            try
            {
                PrintTextControl.DefaultPageSettings = PrintPageSettings;
                PreviewPrint.Document = PrintTextControl;
                PreviewPrint.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void PrintTextControl_PrintPage(object sender, PrintPageEventArgs e)
        {
            PrintTextControl.DocumentName = "Test Document";
            string PrintString = Price.Text;
            Font PrintFont = new Font(Price.Font, Price.Font.Style);
            int numChars = 0;
            int numLines = 0;
            string stringForPage;
            StringFormat strFormat = new StringFormat();

            RectangleF rectDraw = new RectangleF(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height);
            SizeF sizeMeasure = new SizeF(e.MarginBounds.Width, e.MarginBounds.Height - PrintFont.GetHeight(e.Graphics));

            strFormat.Trimming = StringTrimming.Word;
            e.Graphics.MeasureString(PrintString, PrintFont, sizeMeasure, strFormat, out numChars, out numLines);
            //e.Graphics.MeasureString(PrintString, PrintFont, sizeMeasure, strFormat);
            stringForPage = PrintString.Substring(0, numChars);
            e.Graphics.DrawString(stringForPage, PrintFont, Brushes.Black, rectDraw, strFormat);
            if (numChars < PrintString.Length)
            {
                PrintString = PrintString.Substring(numChars);
                e.HasMorePages = true;
            }
            else
            {
                e.HasMorePages = false;
                PrintString = Price.Text;
            }
        }

        private void mode3_CheckedChanged(object sender, EventArgs e)
        {
            textBox_number.Enabled = true;
            textBox_number.BackColor = Color.LightGreen;
        }

        private void mode2_CheckedChanged(object sender, EventArgs e)
        {
            textBox_number.Enabled = false;
            textBox_number.BackColor = Color.White;
        }

        private void mode1_CheckedChanged(object sender, EventArgs e)
        {
            textBox_number.Enabled = false;
            textBox_number.BackColor = Color.White;
        }
        private void textBox_datamatrix_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Convert.ToInt32(e.KeyChar) == 13)
            {
                //connect database => set value on price
                Price.Text = "";
                Random r = new Random();
                int rand = r.Next();
                string url = "http://employee.pharmacity.vn/InGia/PriceService.aspx?datamatrix=" + textBox_datamatrix.Text + "&value=" + (comboBox_pricemode.SelectedIndex + 1) + "&rand=" + rand.ToString();
                string textFromFile = (new WebClient()).DownloadString(url);
                textFromFile = textFromFile.Replace("<span id=\"price\">","");
                textFromFile = textFromFile.Replace("</span>", "");
                textFromFile = textFromFile.Replace(";", Environment.NewLine);
                if (textFromFile.Length > 6)
                {
                    Price.Text = textFromFile;
                    if (mode2.Checked)
                    {
                        try
                        {
                            PrintTextControl.PrintPage += this.TextPrint;
                            SetDefaultPrinter(combo_temprice.SelectedItem.ToString());
                            PrintTextControl.Print();
                            SetDefaultPrinter(combo_makemoney.SelectedItem.ToString());
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                    if (mode3.Checked)
                    {
                        PrintTextControl.PrintPage += this.TextPrint;
                        SetDefaultPrinter(combo_temprice.SelectedItem.ToString()); 
                        for (int i = 0; i < int.Parse(textBox_number.Text); i++)
                        {
                            PrintTextControl.Print();
                        }
                        SetDefaultPrinter(combo_makemoney.SelectedItem.ToString());
                    }
                }
                else
                    MessageBox.Show("Không tìm thấy.....");
                textBox_datamatrix.Text = "";
            }
        }

        private void Price_TextChanged(object sender, EventArgs e)
        {
            textBoxpricemain.Text = Price.Text.Replace(Environment.NewLine, "/");
        }
   
    }
}
